package com.topc.participant.api;

import cn.hutool.core.util.ObjectUtil;
import com.topc.base.api.CommonResult;

import com.topc.base.constant.RedisConstant;
import com.topc.base.model.PageParams;
import com.topc.base.model.PageResult;
import com.topc.base.model.UserAuthDto;
import com.topc.base.utils.TimeUtils;
import com.topc.core.sevice.RedisService;
import com.topc.participant.mapper.UserInfoMapper;
import com.topc.participant.model.dto.QueryUserDto;
import com.topc.participant.model.dto.SaveUserDto;
import com.topc.participant.model.dto.UpdatePwdDto;
import com.topc.participant.model.po.UserInfo;
import com.topc.participant.model.vo.UserShowVo;
import com.topc.participant.service.IUserInfoService;
import com.topc.participant.util.SecurityUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;

@Api("用户业务封闭接口")
@Slf4j
@RestController
@RequestMapping("/close/user")
public class UserCloseController {

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private RedisService redisService;

    @GetMapping("/logout")
    public CommonResult<String> logOut() {
        userInfoService.logout(SecurityUtil.getUser().getNumber());
        return CommonResult.success("退出成功");
    }

    @GetMapping("/info")
    public CommonResult<UserInfo> getUserInfoLocal() {
        UserInfo userInfo = userInfoService.getUserInfoByNumber(SecurityUtil.getUser().getNumber());
        return CommonResult.success(userInfo);
    }

    @PostMapping("/search-info")
    public CommonResult<PageResult<UserShowVo>> searchInfoByPage(PageParams pageParams, @RequestBody @Validated QueryUserDto queryUserDto) {
        PageResult<UserShowVo> pageResult = userInfoService.searchInfoByPage(pageParams, queryUserDto);
        return CommonResult.success(pageResult);
    }

    @PostMapping("/info/save")
    public CommonResult<Long> saveUserInfo(@RequestBody @Validated SaveUserDto saveUserDto) {
        Long newId = userInfoService.saveUserInfo(saveUserDto);
        return CommonResult.success(newId);
    }

    @GetMapping("/lock-info")
    public CommonResult<String> lockUserStatus(@NotNull String number) {
        redisService.hSet(RedisConstant.PEOPLE_LOCK_NUMBER, "user_" + number, TimeUtils.timeFormat(LocalDateTime.now()));
        userInfoService.updateUserStatus(number,1);
        return CommonResult.success("用户上锁成功");
    }

    @GetMapping("/unlock-info")
    public CommonResult<String> unlockUserStatus(@NotNull String number) {
        redisService.hDel(RedisConstant.PEOPLE_LOCK_NUMBER, "user_" + number);
        userInfoService.updateUserStatus(number,0);
        return CommonResult.success("用户解锁成功");
    }

    @PostMapping("/info/update-pwd")
    public CommonResult<String> updateUserPwd(@RequestBody @Validated UpdatePwdDto updatePwdDto) {
        String newPwd = updatePwdDto.getNewPwd();
        String originPwd = updatePwdDto.getOriginPwd();
        String number = SecurityUtil.getUser().getNumber();
        userInfoService.updateUserPwd(number,newPwd,originPwd);
        return CommonResult.success("密码修改成功");
    }

    //批量注册
    @PostMapping("/save-excel")
    public CommonResult<String> saveListByExcel(@RequestParam("file") MultipartFile file) throws Exception{
        userInfoService.saveListByExcel(file);
        return CommonResult.success("批量注册成功");
    }

}
